
package prefuseDemos;


import java.awt.geom.Rectangle2D;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.swing.*;

import prefuse.*;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.assignment.ColorAction;
import prefuse.action.assignment.DataShapeAction;
import prefuse.action.layout.AxisLabelLayout;
import prefuse.action.layout.AxisLayout;
import prefuse.controls.ToolTipControl;
import prefuse.data.Table;
import prefuse.render.AbstractShapeRenderer;
import prefuse.render.AxisRenderer;
import prefuse.render.DefaultRendererFactory;
import prefuse.render.Renderer;
import prefuse.render.RendererFactory;
import prefuse.render.ShapeRenderer;
import prefuse.util.ColorLib;
import prefuse.util.ui.UILib;
import prefuse.visual.VisualItem;
import prefuse.visual.expression.VisiblePredicate;

public class CopyOfanntest2 {

	private static Table generateTable() {
		Table table = new Table();
		String dateText = null;
	
		// set up table schema
		table.addColumn("Date", String.class);
		table.addColumn("TempData", double.class);


		table.addRows(3);
	
		

		dateText = dayTimeToDate(37987);
		table.set(0, 0, dateText);
		table.set(0, 1, 0);



		dateText = dayTimeToDate(38987);
		table.set(1, 0, dateText);
		table.set(1, 1, 35.8);



		dateText = dayTimeToDate(40178);
		table.set(2, 0, dateText);
		table.set(2, 1, 28.8);

		

		return table;
	}
	private static String dayTimeToDate(int dayN) {
		int dayNumber = dayN - 1;
		Calendar origin = Calendar.getInstance();
		origin.set(1900, 00, 00); // init a origin calendar.
		origin.add(Calendar.DAY_OF_YEAR, dayNumber);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = format.format(origin.getTime());
		System.out.println("Today is:" + dateString);
		return dateString;

	}

	private static JComponent createVisualization(Table data) {
		Visualization vis = new Visualization();
		Display display = new Display(vis);

		// --------------------------------------------------------------------
		// STEP 1: setup the visualized data

		vis.add("data", data);

		// --------------------------------------------------------------------
		// STEP 2: set up renderers for the visual data
//
//		vis.setRendererFactory(new DefaultRendererFactory(
//						new ShapeRenderer(7)));//the size of the circle
		
		
		vis.setRendererFactory(new RendererFactory() {
			AbstractShapeRenderer sr = new ShapeRenderer(5);
			Renderer arY = new AxisRenderer(Constants.FAR_LEFT,
					Constants.CENTER);
			Renderer arX = new AxisRenderer(Constants.CENTER,
					Constants.FAR_BOTTOM);

			public Renderer getRenderer(VisualItem item) {
				return item.isInGroup("ylab") ? arY
						: item.isInGroup("xlab") ? arX : sr;
			}
		});

		
		

		// --------------------------------------------------------------------
		// STEP 3: create actions to process the visual data

		AxisLayout x_axis = new AxisLayout("data", "Date", Constants.X_AXIS,
				VisiblePredicate.TRUE);

		AxisLayout y_axis = new AxisLayout("data", "TempData", Constants.Y_AXIS,
				VisiblePredicate.TRUE);
		
		AxisLabelLayout x_labels = new AxisLabelLayout("xlab", x_axis);//You can change the spacing the minimum spacing between axis labels
		//public AxisLabelLayout(String group, AxisLayout layout, Rectangle2D bounds, double spacing)

		AxisLabelLayout y_labels = new AxisLabelLayout("ylab", y_axis);
		

		ColorAction color = new ColorAction("data", VisualItem.FILLCOLOR,
				ColorLib.rgb(100, 100, 255));


		ActionList draw = new ActionList();
		draw.add(x_axis);   //draw point
		draw.add(y_axis);	//draw point
		draw.add(x_labels);
		draw.add(y_labels);
		draw.add(color);
	//	draw.add(shape);
		draw.add(new RepaintAction());
		vis.putAction("draw", draw);

		// --------------------------------------------------------------------
		// STEP 4: set up a display and controls
		display.setHighQuality(true);
		display.setSize(500, 400);

		display.setBorder(BorderFactory.createEmptyBorder(15, 30, 15, 30));

		// --------------------------------------------------------------------
		// STEP 5: launching the visualization
		vis.run("draw");

		return display;
	}

	private static void createAndShowGUI(JComponent display) {
		JFrame frame = new JFrame();

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("p r e f u s e | s c a t t e r   p l o t");

		frame.getContentPane().add(display);

		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		Locale.setDefault(Locale.US);
		UILib.setPlatformLookAndFeel();

		Table table = generateTable();
		final JComponent display = createVisualization(table);

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI(display);
			}
		});
	}

}
